package top.mable.payment.listener;

import cn.hutool.core.util.ObjectUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import top.mable.common.vo.DemoMsg;
import top.mable.payment.service.RabbitMqRetryTimeService;

import java.io.IOException;

/**
 * @Author wen
 * @Date 2021/7/7
 * 死信队列
 **/
@Component
@Slf4j
public class MqFailMsgListener {

    @Autowired
    private RabbitMqRetryTimeService rabbitMqRetryTimeService;

    @RabbitListener(bindings = {@QueueBinding(
            exchange = @Exchange(value = "dead-exchange",type = ExchangeTypes.TOPIC),
            value = @Queue(value = "dead-queue",durable = Exchange.TRUE),
            key = "dead-key")})
    public void process(Message message, Channel channel) throws IOException {
        DemoMsg unserialize = ObjectUtil.unserialize(message.getBody());
        log.info("进入--MqFailMsgListener--死信队列  {}",unserialize.toString());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    /*@RabbitListener(bindings = {@QueueBinding(
            exchange = @Exchange(value = "dead-exchange",type = ExchangeTypes.TOPIC),
            value = @Queue(value = "dead-queue",durable = Exchange.TRUE),
            key = "dead-key")})
    public void process2(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        DemoMsg unserialize = ObjectUtil.unserialize(message.getBody());
        log.info("进入--MqFailMsgListener--死信队列  {}",unserialize.toString());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }*/
}
